package com.alks.function.data.vo.sys;

import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alks.common.utils.export.constants.FileStatusEnum;
import com.alks.entity.data.entity.sys.SysFileExportRecord;
import com.alks.entity.exception.ServiceErrorException;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * @author: chen
 * @date: 2024/12/27
 * @description: 文件导出分页查询VO
 */
@Data
@ApiModel(value = "文件导出分页查询VO")
public class FileExportRecordVO {

    @ApiModelProperty(value = "id")
    @TableId(value = "id",type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "文件桶")
    private String bucket;

    @ApiModelProperty(value = "文件名称")
    private String fileName;

    @ApiModelProperty(value = "文件原始名称")
    private String original;

    @ApiModelProperty(value = "文件的大小（byte)")
    private String fileSize;

    @ApiModelProperty(value = "文件状态 0 生成中 1 待下载 1 已下载")
    private String status;

    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @ApiModelProperty(value = "生成时间")
    private LocalDateTime fileTime;

    static public FileExportRecordVO toVO(SysFileExportRecord record) {
        FileExportRecordVO vo = new FileExportRecordVO();
        vo.setId(record.getId());
        vo.setOriginal(record.getOriginal());
        vo.setBucket(record.getBucket());
        vo.setFileName(record.getFileName());
        vo.setFileTime(record.getCreateTime());
        String size = "未知";
        if (record.getFileSize() != null) {
            if (record.getFileSize() < 1024) {
                size = record.getFileSize() + " B";
            } else if (record.getFileSize() < 1024 * 1024) {
                size = String.format("%.2f KB", record.getFileSize() / 1024.0);
            } else if (record.getFileSize() < 1024 * 1024 * 1024) {
                size = String.format("%.2f MB", record.getFileSize() / (1024.0 * 1024));
            } else {
                size = String.format("%.2f GB", record.getFileSize() / (1024.0 * 1024 * 1024));
            }
        }
        vo.setFileSize(size);
        FileStatusEnum status = FileStatusEnum.getByCode(record.getStatus());
        if (status == null) {
            throw new ServiceErrorException("状态异常");
        }
        vo.setStatus(status.getValue());
        return vo;
    }
}
